package com.yanya.week04.jdk;

import com.yanya.week04.ChainAbleInvocationContext;

import javax.interceptor.InvocationContext;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class CustomInvocationHandler implements InvocationHandler {

    private final Object target;
    private final Object[] interceptors;

    public CustomInvocationHandler(Object target, Object[] interceptors) {
        this.target = target;
        this.interceptors = interceptors;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        InvocationContext delegateContext = new JDKInvocationContext(target, method, proxy,  args);
        ChainAbleInvocationContext context = new ChainAbleInvocationContext(delegateContext, interceptors);
        return context.proceed();

//        System.out.println("Before invocation");
//        Object retVal = method.invoke(target, args);
//        System.out.println("After invocation");
//        return retVal;
    }
}
